<template>
  <div class="container">
    <h2>日志与终端视频下载</h2>

    <!-- 输入框和下载按钮 -->
    <div class="form-group">
      <label for="ipfsHash">请输入 IPFS Hash：</label>
      <input
        v-model="ipfsHash"
        type="text"
        id="ipfsHash"
        placeholder="请输入 IPFS hash"
      />
    </div>

    <div class="button-group">
      <!-- 下载日志按钮 -->
      <button @click="downloadLog">下载日志</button>
      <!-- 下载终端视频按钮 -->
      <button @click="downloadVideo">下载终端视频</button>
    </div>

    <!-- 错误提示 -->
    <div v-if="errorMessage" class="error">
      <p>{{ errorMessage }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ipfsHash: '', // 用户输入的 IPFS hash
      errorMessage: '', // 错误信息
    };
  },
  methods: {
    // 下载日志文件
    downloadLog() {
      if (!this.ipfsHash) {
        this.errorMessage = '请输入有效的 IPFS hash';
        return;
      }

      const url = `http://192.168.231.31:8007/downloadlog?ipfs_hash=${this.ipfsHash}`;
      this.startDownload(url);
    },
    // 下载终端视频文件
    downloadVideo() {
      if (!this.ipfsHash) {
        this.errorMessage = '请输入有效的 IPFS hash';
        return;
      }

      const url = `http://192.168.231.31:8007/downloadlogTar?ipfs_hash=${this.ipfsHash}`;
      this.startDownload(url);
    },
    // 启动下载
    startDownload(url) {
      this.errorMessage = ''; // 清空之前的错误信息

      const a = document.createElement('a');
      a.href = url;
      a.download = ''; // 如果服务器支持 Content-Disposition，浏览器会自动处理文件名
      a.click();
    },
  },
};
</script>

<style scoped>
.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  font-size: 14px;
  font-weight: bold;
}

.form-group input {
  width: 100%;
  padding: 10px;
  font-size: 14px;
  margin-top: 5px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.button-group {
  margin-top: 20px;
  text-align: center;
}

.button-group button {
  padding: 10px 20px;
  font-size: 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background-color: #4CAF50;
  color: white;
  margin-right: 10px;
}

.button-group button:hover {
  background-color: #45a049;
}

.error {
  color: red;
  margin-top: 20px;
  text-align: center;
}
</style>
